package com.wx.commons;

import javax.servlet.http.HttpServletRequest;

import com.wx.core.vo.JsapiTicket;
import com.wx.service.WxJsapiTicketService;
import com.wx.util.StringUtils;
import com.zyht.common.AbsPrintBaseController;

/**
 * <b>描述：</b>	如果需要使用jsApiTicket，继承这个Controller<br/>
 * <b>作者：</b>zrb <br/>
 * <b>修改日期：</b>2016年5月20日 - 下午4:00:14<br/>
 */
public abstract class JsapiTicketController extends AbsPrintBaseController{

	protected abstract WxJsapiTicketService getJsapiTicketService();
	
	protected JsapiTicket getJsapiTicket(String wxid, HttpServletRequest request){
		return getJsapiTicketService().createJsapiTicket(wxid, getRefererUrl(request));
	}
	
	private String getRefererUrl(HttpServletRequest request){
		String refererUrl = request.getScheme().concat("://").concat(request.getServerName()); 
		int port = request.getServerPort();
        if(port>0 && port!=80){
        	refererUrl = refererUrl.concat(":").concat(port+"");
        }
        refererUrl=refererUrl.concat(request.getContextPath()).concat(request.getServletPath());
        String qs;
        if(!StringUtils.isEmpty(qs = request.getQueryString())){
        	refererUrl=refererUrl.concat("?").concat(qs);
        }
    	return refererUrl;
	}
}
